package com.cqlfn.business.data.datapairingfill.dao.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import com.cqlfn.business.busData.dao.syn.SynDao;
import com.cqlfn.business.data.configManagement.entity.CfgColumn;
import com.cqlfn.business.data.configManagement.entity.CfgTable;
import com.cqlfn.business.data.configManagement.repository.CfgColumnRepository;
import com.cqlfn.business.data.configManagement.repository.CfgTableRepository;
import com.cqlfn.business.data.datapairingfill.bean.DataPairingUpload;
import com.cqlfn.business.data.datapairingfill.bean.LatestColumn;
import com.cqlfn.business.data.datapairingfill.dao.DataPairingFillDao;
import com.cqlfn.business.data.erp.DemandSide.service.AsyncQueryDataService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @ClassName DataPairingFillDaoImpl
 * @Description 得到所有的表
 * @Author yuanfeng
 * @Date 2019/8/8 13:56
 * @Version 1.0
 **/

@Repository
public class DataPairingFillDaoImpl implements DataPairingFillDao {

    @Autowired
    private CfgColumnRepository cfgColumnRepository;

    @Autowired
    private CfgTableRepository cfgTableRepository;

    @Autowired
    private SynDao synDaoImpl;

    @Autowired
    private AsyncQueryDataService asyncQueryDataService;

    /**
     * @return 把得到的所有表返回
     * @Description:得到所有表
     * @Param null
     * @date: 2019/8/12 9:52
     */
    @Override
    public Iterable<CfgTable> getAllTables() {

        //得到所有表
        Iterable<CfgTable> allTables = cfgTableRepository.findAll();

        return allTables;
    }


    /**
     * @return 返回字段的一些约束
     * @Description:前端列字段校验
     * @Param tableName 表名
     * @date: 2019/8/12 9:53
     */
    @Override
    public List<Map<String, Object>> validationColumn(String tableName) {

        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();

        List<CfgColumn> cfgColumns = cfgColumnRepository.findByCfgTableTblName(tableName);
        CfgTable tblName = cfgTableRepository.findByTblName(tableName);
        String columnRuleSet = tblName.getCoulmnRuleSet();
        if (StringUtils.isEmpty(columnRuleSet)) {
            resultMap.put("cfgColumns", cfgColumns);
            resultMap.put("listMap", null);
            resultList.add(resultMap);
        } else {
            List<Map<String, Object>> listMap = JSON.parseObject(columnRuleSet, List.class);
            resultMap.put("cfgColumns", cfgColumns);
            resultMap.put("listMap", listMap);
            resultList.add(resultMap);
        }
        return resultList;


    }

    /**
     * @return 返回新增数据成功或者出错的结果
     * @Description:新增数据
     * @Param map 前端传进来的json Map
     * @Param tableName 表名
     * @date: 2019/8/12 9:55
     */
    @Override
    public CfgTable getCfgTable(String tableName) {

        return cfgTableRepository.findByTblName(tableName);
    }

    /**
     * @return 返回分页后的数据
     * @Description:查询分页
     * @Param pageable 分页对象
     * @Param tableName 表名
     * @Param result 存放返回的map数据
     * @date: 2019/8/12 10:12
     */
    @Override
    public Map<String, Object> listByTableName(Pageable pageable,String tableName,Map<String, Object> result) {

        //构建sql语句并查询bus_business数据库中对应的表
        String buildQuerySql = asyncQueryDataService.buildQuerySqlByTableName(tableName,
                pageable.getOffset(),
                pageable.getPageSize());
        //查询medical_business_data中的数据
        List<Map<String, Object>> queryTableData = synDaoImpl.queryTableData(buildQuerySql);

        List<Map<String, Object>> maps = synDaoImpl.queryTableColumnRows("select count(*) total from " + tableName);
        int totalElements = Integer.parseInt(maps.get(0).get("total").toString());
        int totalPages = (totalElements + pageable.getPageSize() - 1) / pageable.getPageSize();
        result.put("data", queryTableData);
        result.put("totalPages", totalPages);
        result.put("totalElements", totalElements);
        result.put("code", 200);
        return result;
    }

    /**
     * @return 返回所有列
     * @Description:获取表的列名
     * @Param tableName 表名
     * @Param list 存放结果map数据
     * @Param map 存放返回数据
     * @date: 2019/8/12 10:43
     */
    @Override
    public List<Map<String, Object>> getColumnName(String  tableName, List<Map<String, Object>> list, Map<String, Object> map) {
        //根据表名得到列
        List<CfgColumn> cfgColumns = cfgColumnRepository.findByCfgTableTblName(tableName);
        for (CfgColumn c : cfgColumns) {
            map.put("key", c.getName());
            map.put("name", c.getDescript());
            map.put("value", "");
            list.add(map);
            //避免key相同的覆盖问题
            map = new HashMap<>();
        }
        return list;
    }

    /**
     * @return 返回详情的所有字段数据
     * @Description:点击显示详情列
     * @Param columnId 列id
     * @Param tableName 表名
     * @Param hashMap 存放结果
     * @date: 2019/8/12 10:45
     */
    @Override
    public List<CfgColumn> listDetail(Map<String, Object> hashMap, Long columnId, String tableName) {

        return cfgColumnRepository.findByCfgTableTblName(tableName);

    }


}